#-*- coding: UTF-8 -*-

from django.shortcuts import render, render_to_response
from django.http import HttpResponse
# Create your views here.
from blog.models import Employee
from django import forms

'''
def index(req):
    return HttpResponse('<h1>Welcome to Django</h1>')
'''


class Person(object):
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex

    def say(self):
        return "I'm " + self.name


def index(req):
    user = {'name': 'tom', 'age': 23, 'sex': 'male'}
    book_list = ['python', 'java', 'php', 'web']
    person = Person('tom', 23, 'male')
    emps = Employee.objects.all()

    # return render_to_response('index.html', {'user': person, 'book_list': book_list})
    return render_to_response('index.html', {'user': emps})


class UserForm(forms.Form):
    # name = forms.CharField(30, help_text='User Register', label='用户名')
    name = forms.CharField(30, label='用户名')
    headImg = forms.FileField(label='头像')


def register(req):
    if req.method == 'POST':
        form = UserForm(req.POST, req.FILES)
        if form.is_valid():
            print form.cleaned_data # dict
            print req.FILES
            fp = file('/tmp/upload/' + form.cleaned_data['headImg'].name, 'wb')
            s = form.cleaned_data['headImg'].read()
            fp.write(s)
            fp.close()
            return HttpResponse('OK')
    else:
        form = UserForm()
        return render_to_response('register.html', {'form': form})

































